<?php

namespace Home\Controller\SaleManager;

use Home\Controller\SaleManager\BasicController;
use Home\Model\SaleManager\City\SaleManagerModel;
use Home\Model\SaleManager\Clinic\ClinicModel;
use Home\Common\SalemanagerHelper;
class LoginController extends BasicController{
	protected  $saleMod;
	function _initialize() {
		$this->saleMod = new SaleManagerModel();

		parent::_initialize($this->saleMod);
        }
	
	public function login(){
		if(IS_POST){
			//判断用户名和密码
			$REQUEST = ( array ) I ( 'request.' );
			//查询电话
			if(is_numeric($REQUEST['phone'])&&!empty($REQUEST['password'])){
				$tuser = $this->saleMod->findInfobyCondition(array('phone'=>$REQUEST['phone']));
				if(!empty($tuser)){
					if($tuser[0]['password']===think_ucenter_md5($REQUEST['password'],UC_AUTH_KEY) && $tuser[0]['status']==0){//登录成功
						$sesseionmane = C('SESSION_PREFIX').'_salemanager';
						session($sesseionmane,null);
						session($sesseionmane,$tuser[0]);
                        cookie('phone',$tuser[0]['phone'], time()+3600*24*365);
                        cookie('password', $tuser[0]['password'], time()+3600*24*365);
						//判断是否第一次登录
						if($tuser[0]['is_login']==0){ //第一次登录
							$this->redirect('/Home/SaleManager/Login/changePassword','',0,'');

						}else{
							if($tuser[0]['role_id']==11){
								$url = SalemanagerHelper::redirectUrl($tuser[0]['role_id'])."indexassistant";
							}else{
								$url = SalemanagerHelper::redirectUrl($tuser[0]['role_id'])."index";
							}
							$this->redirect($url,'',0,'');
						}
						
					}else{
						if($tuser[0]['status']!=0){
							$this->assign('msg','用户被禁用！');
						}else{
							$this->assign('msg','密码错误！');
						}
						$this->assign('status','error');
						
					}
					
				}else{
					$this->assign('status','error');
					$this->assign('msg','该手机号没有注册！');
				}			
			}else{
				$this->assign('status','error');
				$this->assign('msg','请输入正确的帐号密码！');
			}
			$this->assign('data',$REQUEST);
			$this->display();
		}else{
// 			//cookie判断
// 			$where = array('phone'=>cookie('phone'),'password'=>cookie('password'));
// 			$cookieuser = SalemanagerHelper::getSaleByWhere($where);
// 			if(!empty($cookieuser)){
// 				$url = SalemanagerHelper::redirectUrl($cookieuser[0]['role_id'])."index";
// 				$this->redirect($url,'',0,'');exit;
// 			};
			$this->display();
		}
	}	
	
	public function findPassword(){
		if(IS_AJAX){
			$REQUEST = ( array ) I ( 'request.' );
			if(!empty($REQUEST['password']) &&!empty($REQUEST['mobile'])){
				$data['password']=think_ucenter_md5($REQUEST['password'],UC_AUTH_KEY);
				$re = $this->saleMod->updateFieldByCondition(array('phone'=>$REQUEST['mobile']), $data);
				if($re['status']==1){
					responseJson(1, '密码修改成功！');
				}else{
					responseJson(1, '密码修改失败！');
				}
			}else{
				responseJson(1, '参数错误！');
			}
		}else{
// 			$uuid=guid();
// 			session($uuid,$uuid);
// 			$this->assign('uuid',$uuid);
			$this->display();
		}
		
	}
	
	//验证码
	public function verify() {
		ob_clean();
		$config = array(
				'fontSize' => 13, // 验证码字体大小
				'length' => 4, // 验证码位数
				'useNoise' => true, // 关闭验证码杂点
				'useImgBg'=>true
		);
		$verify = new \Think\Verify($config);
		$verify->codeSet = '0123456789';
		$verify->entry();
	}
	/**
	 * 验证图形验证码
	 */
// 	public function code() {
// 		$verify = I('vcode', '', 'htmlspecialchars,trim');
// 		if (!$this->checkVerify($verify)) {
// 			$data['status'] = "0100";
// 		} else {
// 			session($verify,$verify);
// 			//echo session($verify);echo '====';
// 			$data['status'] = "0000";
// 		}
// 		$this->ajaxReturn($data);
// 	}
	//获取uid
	public function code() {
		$uuid=guid();
		session($uuid,$uuid);
		$this->ajaxReturn(array('code'=>200,'uuid'=>$uuid));
	}
	
	//验证码检测
	public function checkVerify($code, $id = '') {
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}
	
	
	/**
	 * 退出操作
	 */
	public function loginOut(){
		$sesseionmane = C('SESSION_PREFIX').'_salemanager';
		session($sesseionmane,null);
        cookie('phone',null);
        cookie('password',null);
		if(IS_AJAX){
			responseJson(3001, '退出成功!','/Home/SaleManager/Login/login');
		}else{
			$this->redirect('/Home/SaleManager/Login/login','',0,'');
		}
		
	}
	
	/**
	 * 修改密码
	 */
	public function changePassword(){
		$sesseionmane = C('SESSION_PREFIX').'_salemanager';
		//print_r(session($sesseionmane)['password']);
		if(IS_AJAX){
			$REQUEST = ( array ) I ( 'request.' );
			if(!empty($REQUEST['originpasswd']) &&!empty($REQUEST['password'])){
				$originpassword=think_ucenter_md5($REQUEST['originpasswd'],UC_AUTH_KEY);
				$data['password']=think_ucenter_md5($REQUEST['password'],UC_AUTH_KEY);
				$data['is_login']=1;
				if(session($sesseionmane)['password']==$originpassword){
					$re = $this->saleMod->updateFieldByCondition(array('phone'=>session($sesseionmane)['phone']), $data);
					if($re['status']==1){
						if(session($sesseionmane)['role_id']==11){
							$url = SalemanagerHelper::redirectUrl(session($sesseionmane)['role_id'])."indexassistant";
						}else{
							$url = SalemanagerHelper::redirectUrl(session($sesseionmane)['role_id'])."index";
						}
						
						responseJson(1, '密码修改成功！',$url);
					}else{
						responseJson(2, '密码修改失败！');
					}
				}else{
					responseJson(2, '原始密码不正确！');
				}
				
			}else{
				responseJson(2, '参数错误！');
			}	
		}else{
			$this->display();
		}
	}
	/**
	 * 意见反馈
	 */
	public function feedback(){
		$REQUEST = ( array ) I ( 'request.' );
		$sesseionmane = C('SESSION_PREFIX').'_salemanager';
		if(IS_POST){
			if(!empty($REQUEST['content'])){
				$data = array(
						'content'=>$REQUEST['content'],
						'contact'=>session($sesseionmane)['phone'],
						'uname'=>session($sesseionmane)['real_name'],
						'role'=>SalemanagerHelper::getRoleName(session($sesseionmane)['role_id'])
						
				);
				$re = D('feedback')->add($data);
				if($re!==false){
					responseJson(1, '我们已经收到您的反馈，谢谢！');
				}
			}else{
				responseJson(2, '参数错误！');
			}
			
		}else{
			$this->display();
		}
		
	}
	
	public function message(){
        $id= self::$user['id'];
		if(IS_POST){
            $value = I('post.value');
            $rst = M('Saleman')->where('id='.$id)->save(array('is_allow'=>$value));
            if($rst===false){
                $this->ajaxReturn(array('code'=>1,'msg'=>'网络繁忙,请稍后再试'));
            }else{
                $sesseionmane = C('SESSION_PREFIX').'_salemanager';
                session($sesseionmane,null);
                $this->ajaxReturn(array('code'=>0));
            }
			
		}else{
            //得到当前状态
            $value = M('Saleman')->field('is_allow')->find($id);
            $value2 =0;
            if($value) $value2 = $value['is_allow'];
            $this->assign('is_allow',$value2);
			$this->display();
		}
	}
}
